package com.example.booksystem.controller;

import com.example.booksystem.Result.Result;
import com.example.booksystem.model.BookInfo;
import com.example.booksystem.model.PageResult;
import com.example.booksystem.model.RequestPage;
import com.example.booksystem.service.BookService;
//import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/addbook")
    public boolean addBook(BookInfo bookInfo){
        //添加日志，还没写

        if(!StringUtils.hasLength(bookInfo.getBookName())
                ||!StringUtils.hasLength(bookInfo.getAuthor())
                ||!StringUtils.hasLength(bookInfo.getPublish())
                ||bookInfo.getCount()==null
                ||bookInfo.getPrice()==null
                ||bookInfo.getStatus()==null){
            return false;
        }

        Integer result = bookService.addBook(bookInfo);
        if(result>0){
            return true;
        }else {
            return false;
        }

    }

    @RequestMapping("/getListPage")
    public PageResult<BookInfo> getListByPage(RequestPage requestPage){
        log.info("getListByPage 接受参数 {}", requestPage);
        PageResult<BookInfo> result=bookService.getListByPage(requestPage);
        return result;

    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        if(bookId==null||bookId<0){
            return new BookInfo();
        }
        return bookService.queryBookById(bookId);
    }
    @RequestMapping("/updateBook")
    public boolean updateBook(BookInfo bookInfo){
        log.info("修改图书:{}",bookInfo);

           bookService.updateBook(bookInfo);
           return true;

    }
    @RequestMapping("/deleteBook")
    public boolean deleteBook(Integer bookId){
        log.info("删除图书 id:{}",bookId);
        bookService.deleteBook(bookId);
        return true;
    }
@RequestMapping("/batchDeleteBook")
    public boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书 ids:{}",ids);
        bookService.batchDeleteBook(ids);
        return true;

    }



}
